<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column
                :index="getIndex"
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAddAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="Hdel(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page"
                :page-sizes="[1, 2, 3, 4]"
                :page-size="pagesize"
                :total="total"
                layout="prev,pager,next,sizes"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      title="编辑弹层"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form ref="roleForm" :rules="ruleForm" :model="roleForm" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog = false">取消</el-button>
          <el-button
            size="small"
            type="primary"
            @click="hSubmit"
          >确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialogAP"
      title="分配权限"
    >
      <assign-permission ref="assignPer" :role-id="roleId" @success="showDialogAP = false" />
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, delRoles, addRole, editRole } from '@/api/setting'
import AssignPermission from './assignPermission.vue'
export default {
  components: {
    AssignPermission

  },
  data() {
    return {
      roles: [],

      total: 0,
      page: 1,
      pagesize: 2,
      showDialog: false,
      showDialogAP: false,
      roleForm: {
        name: '',
        description: ''
      },
      ruleForm: {
        name: [
          { required: true, message: '角色名称必填', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述必填', trigger: 'blur' }
        ]
      },
      isEdit: false,
      roleId: ''
    }
  },
  created() {
    // 初始化角色列表
    this.initRoles()
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.initRoles()
    },
    handleCurrentChange(val) {
      this.page = val
      this.initRoles()
    },
    // 点击分配角色按钮
    hAddAssign(id) {
      // console.log(id)
      this.roleId = id
      this.showDialogAP = true
      this.$nextTick(() => {
        this.$refs.assignPer.getRoleDetail()
      })
    },
    // 关闭弹层清空表单
    hClose() {
      this.$refs.roleForm.resetFields()
      this.roleForm = { name: '', description: '' }
    },
    // 编辑角色
    async doEdit() {
      try {
        // 1、发请求新增角色
        await editRole(this.roleForm)
        // 2、重新获取数据
        this.initRoles()
        // 3、关闭弹层
        this.showDialog = false
        // 4、提示用户编辑成功
        this.$message.success('编辑成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 用户点击编辑按钮
    hEdit(obj) {
      this.showDialog = true
      this.isEdit = true
      this.roleForm = { ...obj }
    },
    // 新增角色
    async doAdd() {
      try {
        // 1、发请求新增角色
        await addRole(this.roleForm)
        // 2、重新获取数据
        //    添加数据之后跳到数据新增的位置
        this.total++
        // Math.ceil 向上取整
        this.page = Math.ceil(this.total / this.pagesize)
        this.initRoles()
        // 3、关闭弹层
        this.showDialog = false
        // 4、提示用户新增成功
        this.$message.success('添加成功')
      } catch (err) { console.log(err) }
    },
    // 用户点击添加按钮
    hAdd() {
      this.isEdit = false
      this.showDialog = true
    },
    // 表单提交按钮
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 自定义序列号
    getIndex(index) {
      return index + (this.page - 1) * this.pagesize + 1
    },
    // 删除
    async doDel(id) {
      try {
        await delRoles(id)
        if (this.roles.length === 1 && this.page > 1) {
          this.page--
        }
        this.$message.success('删除成功')
        this.initRoles()
      } catch (err) { console.log(err) }
    },
    // 删除按钮
    Hdel(id) {
      this.$confirm('确定删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },

    fn(obj) {},
    // 初始化角色列表
    async initRoles() {
      try {
        const res = await getRoles({ page: this.page, pagesize: this.pagesize })
        // console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        this.$message.error(err)
      }
    }
  }
}
</script>
